#ifndef CHESSPROTOMANAGER_H
#define CHESSPROTOMANAGER_H
#include "Chess.h"
#include <iostream>
#include <string>
#include <QFile>
#include <QtXml/qdom.h>
#include <QtXml/QDomElement>
#include <QtXml/QDomDocument>
#include <QtXml/QDomNode>
#include <map>


template<class T>
class ChessProtoManager
{
public:
    ChessProtoManager(){}

    static void registerAllChess(const char* filename){
        QString pathname(":/layout/");
        pathname.append(filename);
        std::cout<<pathname.toStdString()<<std::endl;
        QFile file(pathname);
        QDomDocument doc;
        doc.setContent( &file );
        file.close();
        QDomElement docElem = doc.documentElement();
        QDomNodeList nodeList = docElem.elementsByTagName("Chess");
        if (nodeList.count() > 0)
        {
            for(int i = 0;i < nodeList.count(); i++)
            {
                QString id(nodeList.at(i).attributes().namedItem("id").nodeValue());
                _chessPool[id.toStdString()] = new T;
                for(int j=0;j<nodeList.at(i).attributes().count();j++){
                    QString key(nodeList.at(i).attributes().item(j).nodeName());
                    QString value(nodeList.at(i).attributes().item(j).nodeValue());
                    _chessPool[id.toStdString()]->addAttribute(key.toStdString(),value.toStdString());
                    std::cout<<key.toStdString()<<" "<<value.toStdString()<<std::endl;
                }

            }
        }
    }

    static Chess* getChess(std::string s){
        if(_chessPool[s]==NULL){
            return NULL;
        }

        //std::cout<<_chessPool[s]->getAttribute("img")<<std::endl;
        return _chessPool[s]->Clone();

    }
private:
    static std::map<std::string,Chess*>_chessPool;
};

template<class T>
std::map<std::string,Chess*> ChessProtoManager<T>::_chessPool;

#endif // CHESSPROTOMANAGER_H
